package top.lingkang.alkui.component;

import cn.hutool.core.lang.Assert;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.stage.Popup;
import javafx.stage.Stage;
import top.lingkang.alkui.utils.WindowUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author lingkang
 */
public class AlkToast {
    private Popup popup;
    private String message;
    private Label label = new Label();
    private Stage stage;
    private Pos position;
    private Color color;
    private long time;

    public AlkToast() {
        popup = new Popup();
        position = Pos.CENTER;
        color = Color.WHITE;
        time = 2000l;
    }

    public AlkToast setNode(Node node) {
        stage = WindowUtils.getStage(node);
        return this;
    }

    public void show() {
        Assert.notNull(stage);
        popup.setAutoFix(true);
        popup.setAutoHide(false);

        label.setText(message);
        label.setTextFill(color);//消息字体颜色
        label.setAlignment(position);//居中
        label.setPadding(new Insets(5, 15, 5, 15));
        label.setStyle("-fx-font-size: 14px;-fx-fill:white;-fx-background-color: #303030;-fx-border-radius: 15px;-fx-background-radius: 15px;");//label透明,圆角
        popup.getContent().add(label);
        popup.show(stage);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(() -> {
                    popup.hide();
                    timer.cancel();
                });
            }
        }, time);
    }


    public String getMessage() {
        return message;
    }

    public AlkToast setMessage(String message) {
        this.message = message;
        return this;
    }

    public Pos getPosition() {
        return position;
    }

    public AlkToast setPosition(Pos position) {
        this.position = position;
        return this;
    }

    public Color getColor() {
        return color;
    }

    public AlkToast setColor(Color color) {
        color = color;
        return this;
    }
}
